package com.example.icampusevent;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.icampusevent.jsonobjects.*;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SignupActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        
        // Disable the built-in main action bar
        getActionBar().setDisplayShowHomeEnabled(false);
        getActionBar().setDisplayShowTitleEnabled(false);
        
        ListView signupListView = (ListView)findViewById(R.id.signup_list);
        signupListView.setAdapter(new SignupListViewAdapter(this));
        
        Button button = (Button)findViewById(R.id.signup_button);
        button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				signup();
			}
        });
    }
    
    
    private void signup() {
    	ListView signupListView = (ListView)findViewById(R.id.signup_list);
    	String firstName = ((EditText)signupListView.getItemAtPosition(0)).getText().toString();
    	String lastName = ((EditText)signupListView.getItemAtPosition(1)).getText().toString();
    	String userId = ((EditText)signupListView.getItemAtPosition(2)).getText().toString();
    	String pwd = ((EditText)signupListView.getItemAtPosition(3)).getText().toString();
    	
    	String url = "http://hamedaan.usc.edu:8080/team15/UserServlet?q=signup&userid=" + userId + 
    			"&firstname=" + firstName + "&lastname=" + lastName + "&password=" + pwd;
    	SignupTask task = new SignupTask();
    	task.execute(url);
    }
    
    private class SignupTask extends AsyncTask<String, Integer, Reader> {
	     protected Reader doInBackground(String... url) {
	    	 InputStream source = retrieveStream(url[0]);
	 		 if(source == null) return null;
	 		 
	 		 Reader inputReader = new InputStreamReader(source);
	 		 return inputReader;
	     }

	     protected void onPostExecute(Reader json) {
	 		 Gson gson = new Gson();
			 ReturnStatus status = gson.fromJson(json, ReturnStatus.class);
			 if(status.getStatus().toLowerCase().equals("success")) {
				 toast("Sign Up Successfully!");
				 Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
				 startActivity(intent);
			 }
			 else {
				 toast("Sign Up Unsuccessfully.");
			 }
	     }
	     
	    // Get data stream from server
	 	private InputStream retrieveStream(String url) {
	 		DefaultHttpClient client = new DefaultHttpClient();
	 		HttpGet request = new HttpGet(url);
	 		try {
	 			HttpResponse response = client.execute(request);
	 			final int status = response.getStatusLine().getStatusCode();
	 			if(status != HttpStatus.SC_OK) {
	 				toast("ERROR CODE: " + status + " for URL " + url);
	 				return null;
	 			}
	 			HttpEntity responseEntity = response.getEntity();
	 			return responseEntity.getContent();
	 		}
	 		catch(IOException e) {
	 			request.abort();
	 			toast("ERROR for " + url);
	 		}
	 		
	 		return null;
	 	}
	}
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_signup, menu);
        return true;
    }
    
    public void toast(String s) {
		CharSequence text = s;
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast.makeText(this, text, duration);
		toast.show();
	}
}
